﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQwithArrayOfObjects
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee[] employees = {
                new Employee("Jason","Red",5000M),
                new Employee("Ashley","Green",7600M),
                new Employee("Matthew","Indigo",3587.5M),
                new Employee("James","Indigo",4700.77M),
                new Employee("Luke","Indigo",6200M),
                new Employee("Jason","Blue",3200M),
                new Employee("Wendy","Brown",4236.4M)};//end init list

            Display(employees, "The original array:");

            var between4K6K =
                from e in employees
                where e.MonthlySalaryValue >= 4000M && e.MonthlySalaryValue <= 6000M
                orderby e.MonthlySalaryValue descending
                select e;

            Display(between4K6K, string.Format("Employees earning in the range {0:C}-{1:C} per month",4000M,6000M));

            var nameSorted =
                from e in employees
                orderby e.LastName descending, e.FirstName descending
                select e;

            Console.WriteLine("First employee when sorted by name:");

            if (nameSorted.Any())
            {
                Console.WriteLine(nameSorted.First().ToString()+"p\n");
            }
            else
            {
                Console.WriteLine("not found!\n");
            }

            var lastNames =
                from e in employees
                select e.LastName;

            Display(lastNames.Distinct(), "unique employees last names:");

            var names =
                from e in employees
                select new { e.FirstName, Last = e.LastName };

            Display(names, "Names only:");
            Console.ReadKey();
        }//end main 

        public static void Display<T>(IEnumerable<T> result, string header)
        {
            Console.WriteLine(header);

            foreach (var element in result)
            {
                Console.WriteLine(element);
            }//end for

            Console.WriteLine();
        }//end method Display 
    }
}
